package org.grottarossa.tms.aeroflow.ui.core.viewmanager;

import java.util.ArrayList;
import java.util.Stack;

import com.vaadin.ui.Component;
import com.vaadin.ui.Window;

/**
 * Classe base per le implementazioni di {@link IViewManager}
 *
 * @author 
 */
public abstract class ViewManagerBase implements IViewManager
{
	protected final Stack<Component> m_views = new Stack<Component>();

	private final ArrayList<IViewManagerListener> m_listeners = new ArrayList<IViewManagerListener>();

	public abstract void exitView(Component view);

	public abstract Window enterView(final Component view);

	public Component getCurrentView()
	{
		if (m_views.empty())
			return null;
		return m_views.peek();
	}

	/**
	 * Notifica l'evento {@link IViewManagerListener#entering(Component)} ai listener registrati
	 * @param view
	 */
	protected void fireEnteringEvent(Component view)
	{
		for (IViewManagerListener listener: m_listeners)
		{
			listener.entering(view);
		}
	}

	/**
	 * Notifica l'evento {@link IViewManagerListener#exiting(Component)} ai listener registrati
	 * @param view
	 */
	protected void fireExitingEvent(Component view)
	{
		for (IViewManagerListener listener: m_listeners)
		{
			listener.exiting(view);
		}
	}

	/**
	 * Notifica l'evento {@link IViewManagerListener#showing(Component)} ai listener registrati
	 * @param view
	 */
	protected void fireShowingEvent(Component view)
	{
		for (IViewManagerListener listener: m_listeners)
		{
			listener.showing(view);
		}
	}

	public void addListener(IViewManagerListener listener)
	{
		m_listeners.add(listener);
	}

	public void removeListener(IViewManagerListener listener)
	{
		m_listeners.remove(listener);
	}
}